package com.sunday.common.mq.kafka.study.spring.e6_Receiving_Messages_3_Batch;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.KafkaHeaders;
import org.springframework.kafka.support.SendResult;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * {@link org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration}
 */
@Slf4j
@RestController
@RequestMapping("/test")
public class MQApi {

    @Autowired
    private KafkaTemplate<Object, Object> kafkaTemplate;

    @GetMapping("/send/{topic}/{key}")
    public void send(@PathVariable String topic, @PathVariable String key) throws ExecutionException, InterruptedException {
        CompletableFuture<SendResult<Object, Object>> future;
        future = kafkaTemplate.send(topic, key, key);
        log.info("{}", future.get());
    }

    @GetMapping("/batch/{topic}")
    public void batch(@PathVariable String topic) {
        CompletableFuture<SendResult<Object, Object>> future;
        for (int i = 0; i < 10; i++) {
            future = kafkaTemplate.send(topic, i + "", i + "");
            future.whenComplete((result, throwable) -> log.info("[{}] result : {}, throwable : {}", topic, result, throwable));
        }
    }

    @KafkaListener(
            id = "myId_3",
            topics = "topic3",
            clientIdPrefix = "TEST3",
            batch = "true",
            containerFactory = "kafkaBatchListenerContainerFactory"
    )
    public void listen(
            List<String> data,
            @Header(KafkaHeaders.RECEIVED_KEY) List<String> keys,
            @Header(KafkaHeaders.RECEIVED_PARTITION) List<Integer> partitions,
            @Header(KafkaHeaders.RECEIVED_TOPIC) List<String> topics,
            @Header(KafkaHeaders.OFFSET) List<Long> offsets
    ) {
        log.info("[data] ======= {}", data.size());
//        data.stream().forEach(s -> log.info("[data]-------{}", s));
//        log.info("[keys] ======= {}", keys.size());
//        keys.stream().forEach(s -> log.info("[keys]-------{}", s));
//        log.info("[partitions] ======= {}", partitions.size());
//        partitions.stream().forEach(s -> log.info("[partitions]-------{}", s));
//        log.info("[topics] ======= {}", topics.size());
//        keys.stream().forEach(s -> log.info("[topics]-------{}", s));
//        log.info("[offsets] ======= {}", offsets.size());
//        keys.stream().forEach(s -> log.info("[offsets]-------{}", s));
    }

}
